package it.controllocredito.frontend.activities;

import java.util.ArrayList;
import java.util.List;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.bean.RegistroChiamate;
import it.controllocredito.frontend.util.RegistroChiamateArrayAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class RegistroChiamateActivity extends FragmentActivity {

	private static final int MN_PARAMS = Menu.FIRST;
	private static final int MN_INFO = MN_PARAMS + 1;
	private static final int DROP_TABLE = MN_INFO + 1;
	private static final int CREATE_UPDATE_TABLE = DROP_TABLE + 1;
	private static final int INIZIALIZZA_TABLE_FROM_REGISTRO = CREATE_UPDATE_TABLE + 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.registro_chiamate_activity);

		CalllogPersistenceManager calllogPersistenceManager = new CalllogPersistenceManager(
				this.getApplicationContext());
		List<RegistroChiamate> listaRegistroChiamate = calllogPersistenceManager
				.getRecordsRegistroChiamate(1, 30);

		ArrayList<RegistroChiamate> x = new ArrayList<RegistroChiamate>();

		int i = 0;

		for (RegistroChiamate registroChiamata : listaRegistroChiamate) {
			x.add(registroChiamata);
			i++;
		}

		RegistroChiamateArrayAdapter registroChiamateArrayAdapter = new RegistroChiamateArrayAdapter(
				getApplicationContext(), x);

		final ListView listView = (ListView) findViewById(R.id.mylist);

		// Assign adapter to ListView
		listView.setAdapter(registroChiamateArrayAdapter);

		// FINE LISTVIEW
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		menu.add(0, MN_PARAMS, 0, "Modifica parametri");
//		menu.add(0, MN_INFO, 0, "Info");
//		menu.add(0, DROP_TABLE, 0, "Drop Table");
//		menu.add(0, CREATE_UPDATE_TABLE, 0, "Create Table");
//		menu.add(0, INIZIALIZZA_TABLE_FROM_REGISTRO, 0, "Aggiorna Chiamate");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main_actions, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainFragmentActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}